package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ITransportFormMap;
import com.iemes.entity.LoadOrderFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.MouldUnpassHandleFormMap;
import com.iemes.entity.PUnloadcheckFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.entity.ZInstallcheackFromMap;
import com.iemes.mapper.mould.ITransportMapper;
import com.iemes.mapper.mould.MouldCheckStandardMapper;
import com.iemes.mapper.mould.ZInstallcheackMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;


@RequestMapping("/iTransport/")
@Controller
public class ITransportController extends BaseController {

	@Inject
	private ITransportMapper iTransportMapper;
	
	@Inject
	private ZInstallcheackMapper zInstallcheackMapper;
	
	@Inject
	private MouldCheckStandardMapper mouldCheckStandardMapper;
	
	@RequestMapping("iTransport_list")
	public String examineOrder(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/iTransport/iTransport_list";
	}
	
	@ResponseBody
	@RequestMapping("iTransport_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		ITransportFormMap iTransportFormMap = getFormMap(ITransportFormMap.class);
		List<Map<String, Object>> iTransport = iTransportMapper.getAll(iTransportFormMap);
		LuogicPaging(pageNow, pageSize, iTransport);
		return pageView;
	}
	
	@RequestMapping("iTransport_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询装模运输表
			ITransportFormMap iTransport = iTransportMapper.getById(id);
			model.addAttribute("iTransport",iTransport);
		}
		return Common.BACKGROUND_PATH + "/mould/iTransport/iTransport_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="iTransport_editEntity")
	@SystemLog(module="装模运输",methods="装模运输表-修改装模运输表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		ITransportFormMap iTransportFormMap=getFormMap(ITransportFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		iTransportFormMap.put("beginTime", sdf.format(date));
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		iTransportFormMap.put("beginTimePeopleId", userFormMap.getInt("id"));
		baseMapper.editEntity(iTransportFormMap);
		//添加装模质量检查表
		String iTransportId=iTransportFormMap.get("id").toString();
		ZInstallcheackFromMap check=zInstallcheackMapper.getByItransportId(iTransportId);
		if(check==null || check.size()<=0){
			ZInstallcheackFromMap zInstallcheackFromMap=new ZInstallcheackFromMap();
			zInstallcheackFromMap.put("iTransportId", iTransportFormMap.get("id"));
			zInstallcheackFromMap.put("checkPeopleId", 11);
			baseMapper.addEntity(zInstallcheackFromMap);
			//循环添加卸模前模具质量检查表检查标准
			MouldCheckStandardFormMap mouldCheckStandardFormMap = getFormMap(MouldCheckStandardFormMap.class);
			mouldCheckStandardFormMap.put("status", 1);
			List<Map<String, Object>> getAllItemBy4=mouldCheckStandardMapper.getAllItemBy4(mouldCheckStandardFormMap);
			for(Map<String, Object> map:getAllItemBy4){
				MouldUnpassHandleFormMap mouldUnpassHandleFormMap=getFormMap(MouldUnpassHandleFormMap.class);
				mouldUnpassHandleFormMap.put("standardId",map.get("id"));
				mouldUnpassHandleFormMap.put("checkId", zInstallcheackFromMap.get("id"));
				mouldUnpassHandleFormMap.put("repairPeopleId",11);
				mouldUnpassHandleFormMap.put("supervisorId",11);
				baseMapper.addEntity(mouldUnpassHandleFormMap);
			}
		}
		return "success";
	}
}









